# coding=utf-8
# @time     :2021/12/31
# @Author   :Simple
# @FileName :address_page
# @Project  :selenium_demo
from time import sleep

from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from common.element import GetElements, GetElement


class AddressPage:
    """
    通讯录页
    """
    add_member_btn = GetElements(xpath="//*[@class='qui_btn ww_btn js_add_member']", describe="添加成员")
    continue_btn = GetElements(xpath="//*[@class='qui_btn ww_btn ww_btn_Blue js_btn_continue']", describe="继续按钮")
    username_input = GetElement(id_="username", describe="姓名输入框")
    acctid_input = GetElement(id_="memberAdd_acctid", describe="id输入框")
    phone_input = GetElement(id_="memberAdd_phone", describe="手机号输入框")
    save_btn = GetElements(xpath="//*[@class='qui_btn ww_btn js_btn_save']", describe="保存按钮")

    def __init__(self, driver):
        self.driver = driver

    def add(self, name, acctid, mobile):
        # 点击异常可能性：
        # 1。元素被遮挡
        # 2。元素有多个，需要人工挑选中合适的元素
        # 3。获取的元素已经被刷新，操作时报错StaleElementReferenceException，需要重新获取
        def wait():
            try:
                self.add_member_btn[-1].click()
            except StaleElementReferenceException:
                self.add_member_btn[-1].click()
            return len(self.continue_btn) > 0
        WebDriverWait(self.driver, 10).until(wait)
        self.username_input.send_keys(name)
        self.acctid_input.send_keys(acctid)
        self.phone_input.send_keys(mobile)
        # self.driver.find_element(By.XPATH, "//*[@class='ww_groupSelBtn_add js_show_party_selector']").click()
        # self.driver.switch_to.alert
        self.save_btn[-1].click()




